# 装饰器
from func import read_data
from func import login_user_dict


def auth_login(func):
    def inner(*args, **kwargs):
        if not login_user_dict['username']:
            return False, '请先登录！'
        else:
            return func(*args, **kwargs)

    return inner


def func_choice(tag):
    def wrapper(func):
        def inner(*args, **kwargs):
            data = read_data('info', 'all_info.txt')
            user_dict = data[login_user_dict['username']]
            while True:
                bank_id = input("请输入银行卡号:").strip()  # 判断银行卡号是否为登录用户的
                if bank_id != login_user_dict['bank_id'] and tag != 'transfer':
                    return False, "银行卡号与当前登录用户卡号不相符，请选择转账功能！"
                elif tag == 'transfer' and bank_id not in [value['id'] for key, value in data.items()]:
                    return False, "对方尚未注册该银行卡号！请提示对方进行注册！"
                elif tag == 'withdraw' and bank_id != login_user_dict['bank_id']:
                    return False, f"卡号{bank_id},输入有误，请检查"
                balance = input("请输入金额:").strip()  # 判断金额是否符合格式
                if not balance.isdigit():
                    print(f"请不要输入非法字符，非法输入:{balance}")
                    continue
                balance = int(balance)
                if balance < 0:
                    print("不可以输入负数！")
                elif tag != 'recharge' and balance > user_dict['balance']:
                    print("余额不足！")
                    continue
                if tag != 'recharge':
                    count = 1
                    while True:
                        for_pay_pwd = input("请输入取款密码:")
                        if for_pay_pwd == login_user_dict['bank_pay_pwd']:
                            break
                        elif count == 4:
                            print("密码错误次数已达上限，请重新选择！")
                            break
                        else:
                            print(f"密码错误({count}/3次机会)")
                            count += 1
                            continue
                return func(balance=balance, bank_id=bank_id)

        return inner

    return wrapper
